﻿@using Nop.Web.Framework.Menu;
@{
    //load sitemap
    var siteMap = new Nop.Web.Framework.Menu.XmlSiteMap();
    siteMap.LoadFrom("~/Administration/sitemap.config");
    
    //menu plugin
    var adminMenuPlugins = EngineContext.Current.Resolve<Nop.Core.Plugins.IPluginFinder>()
        .GetPlugins<IAdminMenuPlugin>()
        .Where(x => x.Authenticate())
        .ToList();
}
@helper RenderMenuItem(Nop.Web.Framework.Menu.SiteMapNode item)
{
    string url = null;
    if (!string.IsNullOrEmpty(item.ControllerName) && !string.IsNullOrEmpty(item.ActionName))
    {
        url = Url.Action(item.ActionName, item.ControllerName, item.RouteValues, null, null);
    }
    else if (!string.IsNullOrEmpty(item.Url))
    {
        url = item.Url.StartsWith("~/", StringComparison.Ordinal) ? Url.Content(item.Url) : item.Url;
    }
    <li>
        
        @if (!String.IsNullOrEmpty(url))
        {
            <a href="@url">
                @if (!String.IsNullOrEmpty(item.ImageUrl))
                {
                    <text>
                        <img alt="" src="@Url.Content(item.ImageUrl)" />
                    </text>
                }
                @item.Title
            </a>
        }
        else
        {
            if (!String.IsNullOrEmpty(item.ImageUrl))
            {
                <text>
                    <img alt="" src="@Url.Content(item.ImageUrl)" />
                </text>
            }
            @item.Title
        }
        @if (item.ChildNodes.Any(x => x.Visible))
        {
            <ul>
                @foreach (var childItem in item.ChildNodes.Where(x => x.Visible))
                {
                    @RenderMenuItem(childItem)
                }
            </ul>
        }
    </li>
}

<ul id="admin-menu">
    @foreach (var item in siteMap.RootNode.ChildNodes.Where(x => x.Visible))
    {
        @RenderMenuItem(item)
    }
	@*plugin custom items*@
    @if (adminMenuPlugins.Any())
    {
        var pluginRootMenuItem = new Nop.Web.Framework.Menu.SiteMapNode
        {
            Title = T("Admin.Plugins").Text,
            Visible = true
        };
        foreach (var adminMenuPlugin in adminMenuPlugins)
        {
            pluginRootMenuItem.ChildNodes.Add(adminMenuPlugin.BuildMenuItem());
        }
        @RenderMenuItem(pluginRootMenuItem)
    }
</ul>
<script>
    $(document).ready(function() {
        $("#admin-menu").kendoMenu();
    });
</script>